package com.github.serezhka.airplay.player.gstreamer;

import com.csmk.control.MKSApi;
import lombok.extern.slf4j.Slf4j;

import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

@Slf4j
public class GstMouseMotionListener implements MouseMotionListener {


    private MKSApi mksApi;

    private long lastTriggerTime = 0; // 上次触发时间

    private static final long THROTTLE_INTERVAL = 20;

    private int px;


    public GstMouseMotionListener(MKSApi mksApi) {
        this.mksApi = mksApi;
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        try{
            // 节流一下一些然后 触发移动
            long currentTime = System.currentTimeMillis();
            if (px!=0 && currentTime - lastTriggerTime >= THROTTLE_INTERVAL) {
                lastTriggerTime = currentTime;
                // 触发移动逻辑
                mksApi.move(e.getX(),e.getY());
                px = 0;
            }else if(px == 0){
                px = e.getX();
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }

    }

    @Override
    public void mouseMoved(MouseEvent e) {

    }
}
